package com.witframework.web.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.witframework.core.persistence.OracleBlobHelper;

public class DownLoadFile  extends HttpServlet {

	//Initialize global variables
	  public void init() throws ServletException {
	  }

	//Process the HTTP Get request
	  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	    try {
	      //保存写到浏览器的数据
	      ByteArrayOutputStream baos = new ByteArrayOutputStream();
	      String blobColumn = request.getParameter("blobColumn");
	      String tableName = request.getParameter("tableName");
	      String condition = request.getParameter("condition");
	      String mimeType = request.getParameter("mimeType");
	      String fileName = request.getParameter("fileName");
	      String charset = request.getParameter("charset");
	      if (null == charset) {
	        charset = request.getCharacterEncoding();
	      }
	      boolean isShowInExperlor = false;
	      String strShowInExperlor = request.getParameter("isShowInExperlor");
	      if (null != strShowInExperlor
	          && strShowInExperlor.trim().equals("true")) {
	        isShowInExperlor = true;
	      }

	      //读取子节数据
	      if (null == blobColumn || null == tableName) {
	        //将文件写到浏览器
	        if (null == fileName) {
	          throw new Exception("未指定文件名( fileName)！");
	        }

	        InputStream is = null;
	        File file = new File(fileName);
	        fileName = new String(file.getName().getBytes(), charset);
	        is = new FileInputStream(file);
	        int byteLength = 0;
	        while ( (byteLength = is.available()) > 0) {
	          baos.write(is.read(new byte[is.available()]));
	        }
	        is.close();
	      }
	      else { //将数据库中的ｂｌｏｂ字段写到浏览器
	        OracleBlobHelper ol = new OracleBlobHelper();
	        /**
	         *FIXME 修改该方法，这里参数null为java.sql.Connection
	         */
	        baos.write(ol.getBLOBbyte(tableName, blobColumn, condition,null));
	        if (baos.size() < 1) {
	          throw new Exception("按照给定的条件无法从数据库中取的数据！");
	        }
	      }

	      //设置参数
	      response.reset();
	      //MimeType
	      String strContentTypeTemp = mimeType;
	      if (null == mimeType) {
	        strContentTypeTemp = "charset=" + charset;
	      }
	      else {
	        strContentTypeTemp = mimeType + ";charset=" + charset;
	      }
//	      response.setContentType(strContentTypeTemp);
	      //fileName
	      String strContentTemp = null;
	      if (isShowInExperlor) {
	        strContentTemp = "attachment;";
	      }
	      else {
	        strContentTemp = "inline;";
	      }
	      if (null != charset) {
	        String strFileNameTemp = new String(fileName.getBytes(),
	                                            "UTF-8");
	        strContentTemp = strContentTemp + "filename=" + strFileNameTemp;
	      }
	      else {
	        strContentTemp = strContentTemp + "filename=" + fileName;
	      }
	      response.setHeader("Content-Disposition", strContentTemp);
	      response.setContentLength(baos.size());

	      //写到浏览器
	      OutputStream os = response.getOutputStream();
	      baos.flush();
	      os.write(baos.toByteArray());
	      baos.close();
	      os.flush();
	      os.close();
	    }
	    catch (Exception e) {
	      e.printStackTrace();
	    }
	  }

	//Process the HTTP Post request
	  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    doGet(request, response);
	  }

	//Clean up resources
	  public void destroy() {
	  }
}

